package com.wing.product.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.annotation.RequestObjectFieldIgnore;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.product.model.entity.WarehouseProductProducer;
import com.wing.product.service.WarehouseProductProducerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Api(value = "云仓", tags = "#/warehouseFactory/product/warehouseProductProducer")
@RestController("WarehouseProductProducerController")
@RequestMapping("/warehouse/admin/productProducer")
@RequiredArgsConstructor
public class WarehouseProductProducerController {

    private final WarehouseProductProducerService warehouseProductProducerService ;


    @ApiOperation(value = "查询所有生产商", notes = "搜索")
    @GetMapping("/list")
    public PageResult<WarehouseProductProducer> list(
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page
            ,@RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit
            ,@RequestParam(value = "sort", defaultValue = "create_time", required = false) String sort
            ,@RequestParam(value = "order", defaultValue = "asc", required = false) String order,
            String producerName,
            String producerCode
    ){

        Page<WarehouseProductProducer> pg = new Page<>(page, limit);
        Map paramMap=new HashMap();
        paramMap.put("sort", StrUtil.toUnderlineCase(sort));
        paramMap.put("order", order);
        paramMap.put("producerName", producerName);
        paramMap.put("producerCode", producerCode);
        //搜索
        IPage<WarehouseProductProducer> list = warehouseProductProducerService.pageList4app(pg,paramMap);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "添加生产商", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@RequestBody WarehouseProductProducer warehouseProductProducer)  {
        try{
            return warehouseProductProducerService.saveWarehouseProductProducer(warehouseProductProducer);
        }catch (Exception e){
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "修改生产商", notes = "修改")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PutMapping("/update")
    public JsonResult update(@RequestBody WarehouseProductProducer warehouseProductProducer) {
        try {
            return warehouseProductProducerService.update(warehouseProductProducer);
        } catch (Exception e) {
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "删除生产商", notes = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Id", value = "品种id", required = true, dataType = "Integer", paramType = "path")
    })
    @DeleteMapping("/{id}" )
    public JsonResult delete(@PathVariable("id") Long warehouseProductProducerId) {
        return warehouseProductProducerService.deleteWarehouseProductProducer(warehouseProductProducerId);
    }
}
